<?php
// +----------------------------------------------------------------------
// |运营后台登录控制器
// +----------------------------------------------------------------------
// |Data:2014-06-23-上午11:42
// +----------------------------------------------------------------------
// |Author: 吕志雄 <870923001@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Common\Controller\AdminCommonController;
use Common\Controller\CommonController;
use Think\Auth;
use Think\Controller;
use Think\Verify;

class PublicController extends AdminCommonController{
    protected $auth;

    protected function _initialize(){

    }

    /**
     * 登录页面显示
     */
    public function index(){
        if($_SESSION[C('USER_AUTH_KEY')]){
            redirect(C('TMPL_PARSE_STRING.__ADMIN__'));
        }else{
            $this->display('login');
        }
    }

    public function login(){
        $this->display('login');
    }

    /**
     * 登录检测
     */
    public function checkLogin(){
        if(IS_POST){
            $data = $this->checkPostData();
            $account = $data['account'];
            $password = $data['password'];

            $this->auth = new Auth();
            $authInfo = $this->auth->getUserInfoS($account,$password);
            if(!$authInfo) {
                $this->error('账号或密码有误！');
            }elseif($authInfo['status']==0){
                $this->error('帐号已被禁用！');
            }else {
                if($authInfo['password'] != $password) {
                    $this->error('密码错误！');
                }

                $_SESSION[C('USER_AUTH_KEY')]= $authInfo['id'];
                $_SESSION['account'] = $authInfo['account'];

                if($authInfo['id']==C('MANAGE_id')) {
                    $_SESSION['administrator'] = true;
                }else{
                    $role_id = M('auth_group_access')->where('uid='.$authInfo['id'])->cache(5)->getField('group_id');
                    $_SESSION['role_id'] = $role_id;
                }

                $this->addHistoryLog('登录成功！');
                $this->success('登录成功！',U('Admin/Index/index'));
            }
        }else{
            $this->error('提交方式错误！');
        }
    }

    /**
     * 检测登录提交的数据
     * @return array
     */
    private function checkPostData(){
        $account = I('post.account','','trim');
        $password = I('post.password','','md5');
        $verify = I('post.verify','','trim');

        if(empty($account)) {
            $this->error('请填写登录帐号！');
        }elseif (empty($password)){
            $this->error('请填写登录密码！');
        }elseif(empty($verify)){
            $this->error('验证码不能为空！');
        }

        $result = check_verify($verify,1);
        if(!$result){
            $this->error('验证码错误！');
        }

        return array('account'=>$account,'password'=>$password);
    }

    /**
     * 验证码显示
     */
    public function verify(){
        $verify = new Verify();
        $verify->entry(1);
    }


    /**
     * 检测验证码
     */
    public function checkVerify(){
        $code = I('verify',0,'trim');
        empty($code) && $this->error('请输入验证码！');
        $result = check_verify($code,1);

        if($result){
            $this->returnData('',1);
        }else{
            $this->returnData('',0);
        }
    }

    /**
     * 退出
     */
    public function logout(){
        if($_SESSION[C('USER_AUTH_KEY')]){
            $this->addHistoryLog('退出成功！');
            session(C('USER_AUTH_KEY'),null);
            session("administrator",null);
            session('account',null);
            session('role_id',null);
            session(null);
            session('[destroy]');
            if(IS_AJAX){
                $this->returnData('退出成功！',1,1);
            } else {
                redirect(U('Admin/Public/index'));
            }
        }else{
            $this->returnData('您已退出登录！',1,1);
        }
    }

    public function  updateinfo(){
        $this->display();
    }

    //修改密码执行方法
    public function doUpdateInfo(){
        if(!$_SESSION[C('USER_AUTH_KEY')]){
            $this->returnData('修改密码失败！',0,0); exit;
        }
        $old_password =I("post.old_password","",'md5') ;
        $que_password =I("post.que_password","",'md5') ;
        $password = M('manage')->where('id='.$_SESSION [C('USER_AUTH_KEY')])->cache(5)->getField('password');
        if($old_password!=$password) $this->returnData('原密码错误！',0,0);
        $res = M('manage')->where('id='.$_SESSION [C('USER_AUTH_KEY')])->setField('password',$que_password);
        if($res!==false){
            $this->returnData('修改密码成功！',1,1);
        }else{
            $this->returnData('修改密码失败！',0,0);
        }
    }
}
?>